/*
    Superjuego Engine
    Copyright (C) 2011 Jorge Luís Cabral y Gabriel Ernesto Cabral

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Contact: baigosoft@hotmail.com
*/

#include <stdio.h>
#include <stdlib.h>
#include "super.h"


#ifdef DREAMCAST
extern uint8 romdisk[];
KOS_INIT_FLAGS(INIT_DEFAULT | INIT_MALLOCSTATS);
KOS_INIT_ROMDISK(romdisk);
#endif



/*creamos sprite*/ 
SE_sprite *mario;
SE_ani *mario_ani;
SE_ani *mario_arr[1];
int posx,posy,movy;
int rotated=1;
int rotate=0;
int lookingright=1;
float rot=0.0f;
float alpha=1.0f;
float zoom=1.0f;
float zoominc=0.0f;
float alphainc=0.0f;
float rotinc=10.0f;


SE_input_joystick *joystick;
SE_input_state *state;
SE_font *fuente;



void cargarsprites()
{

	int i=0;
	for(i;i<1;i++){
		posx= 0;
		posy= 0;
		

		/*iniciamos el sprite (tiene un solo frame)*/
		mario_arr[i] = SE_ani_init(2);
#ifdef DREAMCAST
		/*cargamos las imagenes*/
		SE_ani_addframe(mario_arr[i],"/rd/run01.png",0,50);
		SE_ani_addframe(mario_arr[i],"/rd/run02.png",1,20);
#else
		SE_ani_addframe(mario_arr[i],"romdisk/run01.png",0,50);
		SE_ani_addframe(mario_arr[i],"romdisk/run02.png",1,20);
#endif
		/*seteamos la posicion*/
		SE_ani_position(mario_arr[i],posx,posy,0);
		SE_ani_zoom(mario_arr[i],1.0);
		SE_ani_delaypercent(mario_arr[i],0.1);


	}


}


void cargarfuente()
{
#ifdef DREAMCAST
fuente = SE_font_create("/rd/font512.png",16,16,32,32);
#else
fuente = SE_font_create("romdisk/font512.png",16,16,32,32);
#endif
SE_font_position(fuente,50,50);
SE_font_settings(fuente,100);

}


void initEngine()
{
SE_utils_log("iniciando demo");
SE_initgraphics();
SE_audio_init(44100, 16, 1);
SE_input_init();
printf("hay %d joysticks disponibles\n",SE_input_enum());
joystick = SE_input_open(0);

}


void mainScene()
{




int quit = 0;
int cont = 0;


#ifdef DREAMCAST
SE_music *musica=SE_audio_loadmusic("/rd/musica.ogg");
#else
SE_music *musica=SE_audio_loadmusic("romdisk/musica.mp3");
#endif
SE_audio_playmusic(musica);


cargarsprites();
				

SE_timer_setframerate(60);
SE_timer_reset();




	while(quit == 0)
	{
			/*borra la pantalla con un color*/
			SE_clearscreen(1.0f,1.0f,1.0f,0.0f);
			
			
			//rotinc=0;
			movy=0;
			zoominc=0;
			state=SE_input_poll(&joystick);
			if(state){	
        			

					if(state->buttons & JOY_RIGHT){  /* Handle Joystick Motion */
							rotate=1;
							posx=posx+5;//rotinc=10.0f;
							//printf("right\n");
					}else if(state->buttons & JOY_LEFT){  /* Handle Joystick Motion */
							rotate=-1;//rotinc=-10.0f;
							posx=posx-5;
							//printf("left\n");
					} //rotinc=0.0f;
					if(state->buttons & JOY_UP)  /* Handle Joystick Motion */
							movy=-5;
					else if(state->buttons & JOY_DOWN) /* Handle Joystick Motion */
							movy=5;
					else movy=0;
					if(state->buttons & JOY_BUTTON0)  /* Handle Joystick Motion */
							zoominc=0.1f;
					if(state->buttons & JOY_BUTTON1) /* Handle Joystick Motion */
							zoominc=-0.1f;
					if(state->buttons & SE_QUIT) /* Handle Joystick Motion */
							quit=1;
				


			}

				//printf("right:%d\nrotate:%d\nrotated:%d\nrot:%f\n",lookingright,rotate,rotated,rot);
				//min 0.0f
				//max -180.f

				if((lookingright)&&(rotated)&&(rotate==-1)){
					rotated=0;
					lookingright=0;
					rot=rot-rotinc;
				}
				if((lookingright)&&(rotated==0)&&(rotate==-1)){
					lookingright=0;
					rot=rot-rotinc;
				}
				if((lookingright==0)&&(rotated)&&(rotate==1)){
					rotated=0;
					lookingright=1;
					rot=rot+rotinc;
				}
				if((lookingright==0)&&(rotated==0)&&(rotate==1)){
					lookingright=1;
					rot=rot+rotinc;
				}
				if((lookingright)&&(rotated==0)&&(rot>0.0f)){
					rotate=0;
					rotated=1;
				}
				if((lookingright==0)&&(rotated==0)&&(rot<-180.0f)){
					rotate=0;
					rotated=1;
				}
				if((lookingright==0)&&(rotated==0)){
					rot=rot-rotinc;
				}
				if((lookingright)&&(rotated==0)){
					rot=rot+rotinc;
				}
				

				if((zoom+zoominc)>0)zoom=zoom+zoominc;
				if(((alpha+alphainc)<=1.0f)&&((alpha+alphainc)>=0.0f))alpha=alpha+alphainc;
				posy=posy+movy;

				SE_ani_rotation(mario_arr[0],0.0f,rot,0.0f);
				SE_ani_position(mario_arr[0],posx,posy,0);
				SE_ani_zoom(mario_arr[0],zoom);
				//SE_ani_alpha(mario_arr[0],alpha);
				SE_ani_draw(mario_arr[0]);

		
		
			
			/*flipea la escena o cuadro*/	
			SE_flip();
			SE_timer_waitframerate();

	}


}


void startScene()
{

int quit=0;
cargarfuente();

	while(quit == 0)
	{
			/*borra la pantalla con un color*/
			SE_clearscreen(0.0f,0.0f,0.0f,0.0f);
			SE_font_draw(fuente,"SUPERJUEGO\nDEMO.1\nSPRITE.ZOOM.JOYSTICK\nDEMO.BY.GUARIPOLO\nGRAPHICS.BY.MACOY\n\MUSIC.BY\nGABRIEL.ALVAREZ\n\n\n\PRESS.START",0.0f,0.0f,0);
			
			state=SE_input_poll(&joystick);

				if(state->buttons & JOY_BUTTON6)  /* Handle Joystick Motion */
							quit=1;
				if(state->buttons & SE_QUIT) /* Handle Joystick Motion */
							quit=1;

			


	SE_flip();

	}


}

int main(int argc,char* args[])
{
initEngine();
startScene();
mainScene();
return 0;

}

